package test.jb698.codec.msm.demo;

import java.util.ArrayList;
import java.util.List;

import cn.jb698.codec.msm.generator.v5base.BcrudCfgObjCreateService;
import cn.jb698.codec.msm.generator.v5base.BcrudCfgObjCreateServiceMysqlImpl;
import cn.jb698.codec.msm.generator.v5base.BcrudCreateCodeFilesImpl;
import cn.jb698.codec.msm.generator.v5base.beans.BcrudCfgTableObj;
import cn.jb698.codec.msm.generator.v5base.beans.BcrudConfigBean;
import cn.jb698.codec.msm.generator.v5base.beans.FileOutputTpltCfg;
import cn.jb698.codec.msm.util.CodecMsmFileOutputTpltUtils;
import test.jb698.codec.util.CodecPropertiesUtils;

/**
 * 
 * @author Administrator(LiuJianbin)
 * @date 2020-05-02
 */
public class ZCodecMysqlProjmdsUsrctrBcrudService {

	public static void main(String[] args) throws Exception {
		
	    BcrudConfigBean configBean = doBcrudConfigBeanInit();

	    // 构建实现类
	    BcrudCfgObjCreateService bcrudCfgObjCreateService = new BcrudCfgObjCreateServiceMysqlImpl();
		List<BcrudCfgTableObj> cfgTableList = bcrudCfgObjCreateService.doBcrudCfgTableObjListCreate(configBean);
		{
			// 二次修改表及字段配置数据,覆盖默认生成的配置信息
		}
		// 最终配置输出到Json文件
		bcrudCfgObjCreateService.doCfgTableObjListOutputToJsonFiles(configBean,cfgTableList); // 输出表信息JSON到配置的输出目录

		// 输出创建的代码
		for(BcrudCfgTableObj tableJsBean : cfgTableList ){
			BcrudCreateCodeFilesImpl bcrudCreateCodeFile = new BcrudCreateCodeFilesImpl();

	        // 初始化Baomidou配套模板
			List<FileOutputTpltCfg> fileTpltList4Bmd = CodecMsmFileOutputTpltUtils.doFileOutputTpltCfgAppend4Bmd(configBean, configBean.getFileOutputTpltCfgList());
			configBean.setFileOutputTpltCfgList(fileTpltList4Bmd);
			
			// 输出模板 confBean.getProjRootDir()
			bcrudCreateCodeFile.doCodeFilesCreateAndOutput(configBean, tableJsBean); 
		}
	}

	/**
	 * 初始化库配置.
	 */
	private static BcrudConfigBean doBcrudConfigBeanInit() {

		String default_driverClassName = CodecPropertiesUtils.getPropVal("spring.datasource.driver-class-name");
		String default_connectUrl = CodecPropertiesUtils.getPropVal("spring.datasource.url");
		String default_userName = CodecPropertiesUtils.getPropVal("spring.datasource.username");
		String default_password = CodecPropertiesUtils.getPropVal("spring.datasource.password");

//		{
//			default_driverClassName = "com.mysql.cj.jdbc.Driver";
//			default_connectUrl = "jdbc:mysql://xx.xx.xx:port/db_name?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false";
//			default_userName = "MyUserName";
//			default_password = "MyPassword";
//		}

		String basePackageName = "cn.jb698.projname.subproj.dbcrud";
		String xmlMapperPackage = "mybatis.projname.subproj.mapper";

		String projRootDir = "D:/Develope/workspace/codec/jb698-projname-subproj"; // 目标项目根路径.
		String jsonFile_outDir = projRootDir + "/table_jsons/dbmysql/";
		String codeOutDir = projRootDir + "/src/main/java/";
		String resOutDir = projRootDir + "/src/main/resources/";
		String pageOutDir = projRootDir + "/src/main/webapp/WEB-INF/jsp/temp_aceadmin/bcrud/";

		List<BcrudCfgTableObj> lmtCfgTables = new ArrayList<>();

		{
			// 
			BcrudCfgTableObj tmp_table = new BcrudCfgTableObj("dt_table_name", "t").doHasSwaggerUi(true).doHasBaomidou(true); // 表名称
			lmtCfgTables.add(tmp_table); 
		}

		BcrudConfigBean confBean = new BcrudConfigBean();
		confBean.setDbDriverClassName(default_driverClassName);
		confBean.setDbConnectionUrl(default_connectUrl);
		confBean.setDbUserName(default_userName);
		confBean.setDbPassword(default_password);

		confBean.setBasePackageName(basePackageName);
		confBean.setProjRootDir(projRootDir);
		confBean.setJavaCodeRootDir(codeOutDir);
		confBean.setResCodeRootDir(resOutDir);
		confBean.setMapperXmlPackageName(xmlMapperPackage);
		confBean.setPageCodeRootDir(pageOutDir);
		confBean.setJsonFileOutDir(jsonFile_outDir);
		confBean.setCfgTables(lmtCfgTables);

        // 初始化Baomidou配套模板
        confBean.setFileOutputTpltCfgList(CodecMsmFileOutputTpltUtils.doFileOutputTpltCfgAppend4Bmd(confBean, confBean.getFileOutputTpltCfgList()));

		return confBean;

	}

}
